from sqlalchemy import Table, Unicode, UnicodeText, Column, ForeignKey
from sqlalchemy.orm import mapper

from tortuga.database import metadata
from tortuga.models.revision import Revision

__all__ = ['Template', 'template_table']

template_table = Table('template', metadata,
    Column('revision_id', None, ForeignKey('revision.revision_id'), primary_key=True),
    Column('content_id', None, ForeignKey('content.content_id')),
    Column('title', Unicode(200), nullable=False),
    Column('engine', Unicode(50), nullable=False),
    Column('path', Unicode(1000), default=None, nullable=True),
    Column('code', UnicodeText(), default=None, nullable=True)
)

class Template(Revision):
    """Represents a web template"""
    def new_revision(self, _readonly_properties=None, 
        _revisioned_onetoone_relations=None, **kwargs):
        """Return a new revision of this Template."""
        # Determine which properties *not* to copy over to a new instance
        readonly_properties = []
        if _readonly_properties: 
            readonly_properties.extend(_readonly_properties)
        # Specify which one_to_one relations should be revisioned
        revisioned_onetoone_relations = []
        if _revisioned_onetoone_relations:
            revisioned_onetoone_relations.extend(_revisioned_onetoone_relations)
        # Call the Revision machinery to do the heavy lifting
        return super(Template, self).new_revision(
            readonly_properties, revisioned_onetoone_relations, **kwargs
        )

mapper(Template, template_table, inherits=Revision, polymorphic_identity=u'template')